package com.cws.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.cws.model.User;

public class BeanUtil {
	
	
	public static List<String> findChangeProperty(Class classz,Object source,Object target){
		return findChangeProperty(classz,source,target,null);
	}
	
	public static List<String> findChangeProperty(Class classz,Object source,Object target,String[] ignoreProperties){
		List<String> list = new ArrayList<String>();
		PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(classz);
		
		for (PropertyDescriptor propertyDescriptor : descriptors) {
			Method readMethod = propertyDescriptor.getReadMethod();
			String propertyName = propertyDescriptor.getName();
			List<String> ignorePro = Arrays.asList(ignoreProperties);
			if(ignorePro.contains(propertyName)){
				try {
					Object sValue = readMethod.invoke(source);
					Object tValue = readMethod.invoke(target);
					if(isChange(sValue, tValue)){//标记
						list.add(propertyName);
					}
					
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	
	
    public static boolean isChange( Object object1,  Object object2) {
        if (object1 == null &&object2 == null) {
            return false;
        }else if(object1 != null &&object2 == null){
        	return true;
        }else if(object1 == null &&object2 != null){
        	return true;
        }else {
        	return  object1.equals(object2);
        }
       
    }
	
	
	public static void main(String[] args) {
		Date d1= new Date();
		Date d2= new Date();
//		findChangeProperty(Date.class, d1, d2);
		BigDecimal b= new BigDecimal("3.0");
		DecimalFormat df = new DecimalFormat("#.00");
		System.out.println(df.format(b));
	}
	
	
	
	
	
	
	
	
	
}
